package org.zoomdev.zoom.protobuf.modules;

import org.zoomdev.zoom.common.utils.Utils;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Collection;

public class SchemaUtils {
    /**
     * 是否是具体的类?
     *
     * @param clazz
     */
    static boolean isSpecific(Class clazz) {
        if (clazz == Object.class) {
            return false;
        }
        //丢失了泛型
        if (Iterable.class.isAssignableFrom(clazz)) {
            return false;
        }
        if (clazz.isArray()) {
            return isSpecific(clazz.getComponentType());
        }
        return true;
    }

    /**
     * 目前暂时只支持Iterable
     *
     * @param type
     * @return
     */
    public static boolean checkParameterizedType(ParameterizedType type) {
        if (Utils.containTypeParameters(type)) {
            return false;
        }
        Type rawType = type.getRawType();
        if (!(rawType instanceof Class)) {
            return false;
        }
        Class<?> rawClass = (Class<?>) rawType;
        if (!Collection.class.isAssignableFrom(rawClass)) {
            return false;
        }

        Type[] a = type.getActualTypeArguments();
        if (a.length > 1) {
            return false;
        }

        Type at = a[0];
        return at instanceof Class;
    }
}
